#include "Input.h"

#include "OGRE/OgreRenderWindow.h"
#include "OGRE/OgreRoot.h"

Input::Input(void)
{
	m_inputManager = nullptr;
	m_window = nullptr;
	m_mouse = nullptr; 
	m_keyboard = nullptr;
	m_root = nullptr;
}


Input::~Input(void)
{
	destroy(true);
}

//Adjust mouse clipping area 
void Input::windowResized(Ogre::RenderWindow* rw)
{
    unsigned int width, height, depth;
    int left, top;
    rw->getMetrics(width, height, depth, left, top);
 
    const OIS::MouseState &ms = m_mouse->getMouseState();
    ms.width = width;
    ms.height = height; 
}
 
//Unattach OIS before window shutdown (very important under Linux) 
void Input::windowClosed(Ogre::RenderWindow* rw) 
{ 
    //Only close for window that created OIS (the main window in these demos) 
    if( rw == m_window) 
    { 
        if( m_inputManager ) 
			destroy(false);
    } 
}

bool Input::frameRenderingQueued(const Ogre::FrameEvent& evt) 
{ 
    if(m_window->isClosed()) 
        return false;
 
    //Need to capture/update each device 
    m_keyboard->capture(); 
//    m_mouse->capture();
 
    return true; 
}

void Input::init(std::string hWnd, Ogre::RenderWindow* window, Ogre::Root *root)
{
	m_window = window;
	listen();

	OIS::ParamList paramList;
	paramList.insert(OIS::ParamList::value_type("WINDOW", hWnd.c_str()));
 
	m_inputManager = OIS::InputManager::createInputSystem(paramList);

	m_mouse = static_cast<OIS::Mouse*>(m_inputManager->createInputObject(OIS::OISMouse, false));
	m_keyboard = static_cast<OIS::Keyboard*>(m_inputManager->createInputObject(OIS::OISKeyboard, false));

	m_root = root;
	m_root->addFrameListener(this);
}

void Input::listen()
{
	//Set initial mouse clipping size windowResized(mWindow);
	//Register as a Window listener 
	Ogre::WindowEventUtilities::addWindowEventListener(m_window, this);
}
void Input::unlisten()
{
	Ogre::WindowEventUtilities::removeWindowEventListener(m_window, this); 
	windowClosed(m_window);
}
void Input::destroy(bool callUnlisten)
{
	dropwindow(callUnlisten);

	if (m_mouse)
	{
		m_inputManager->destroyInputObject(m_mouse); 
		m_mouse = nullptr;
	};

	if (m_keyboard)
	{
		m_inputManager->destroyInputObject(m_keyboard);
		m_keyboard = nullptr;
	};

	if (m_inputManager)
	{
		OIS::InputManager::destroyInputSystem(m_inputManager); 
		m_inputManager = nullptr; 
	};

	if (m_root)
	{
		m_root->removeFrameListener(this);
		m_root = nullptr;
	}
}

inline void Input::dropwindow(bool callUnlisten)
{
	if (m_window)
	{
		if (callUnlisten)
			unlisten();
		m_window = nullptr;
	};
}

void Input::update()
{
	m_keyboard->capture(); 
    m_mouse->capture();
}

bool Input::isKeyDown(OIS::KeyCode key)
{
	if (m_keyboard->isKeyDown(key)) // Left - yaw or strafe
		return true;

	return false;
/*	{
		if(m_keyboard->isKeyDown( OIS::KC_LSHIFT ))
		{
			// Yaw left
			mSceneMgr->getSceneNode("NinjaNode")->yaw(Ogre::Degree(mRotate * 5));
		} else {
			transVector.x -= mMove; // Strafe left
		}
	}
	if (mKeyboard->isKeyDown(OIS::KC_L)) // Right - yaw or strafe
	{
		if(mKeyboard->isKeyDown( OIS::KC_LSHIFT ))
		{
			// Yaw right
			mSceneMgr->getSceneNode("NinjaNode")->yaw(Ogre::Degree(-mRotate * 5));
		} else {
			transVector.x += mMove; // Strafe right
		}
	}
	*/
};